import React, { useEffect, useRef } from 'react';
import { marked } from 'marked';
import mermaid from 'mermaid';
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';

import './markdown.scss';

// 创建一个全局的图表缓存
const diagramCache = new Map();

// 自定义渲染器
const customRenderer = new marked.Renderer();

// 覆盖 code 方法
function isMermaidCandidate(code) {
    const typeLine = code.trim().split('\n')[0].trim();
    return /^(flowchart|graph|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|gitGraph|mindmap|timeline|graph TD|graph LR)$/i.test(typeLine);
}
customRenderer.code = function(obj) {
    const raw = obj.raw;
    const code = obj.text;
    const language = obj.lang;
    // 处理 mermaid 代码块
    if (language === 'mermaid' && raw.trim().endsWith("```") && isMermaidCandidate(code)) {
        let cleanCode = code.replace(/^flowchart-link\s*/, '')  // 移除开头的 flowchart-link
            .replace(/^#.*$/gm, '')  // 移除注释行
            .replace(/^\s*style\s*{[\s\S]*?}\s*$/gm, '')  // 移除样式块
            .replace(/^\s*click\s+.*$/gm, '')  // 移除点击事件
            .trim();

        // 对于 pie 图表，保留中文标签的引号
        if (!cleanCode.includes('pie')) {
            // 其他图表类型移除所有引号
            cleanCode = cleanCode.replace(/['"]/g, '');
        }

        // 检查缓存中是否有相同的图表
        if (diagramCache.has(cleanCode)) {
            return diagramCache.get(cleanCode).outerHTML;
        }

        // setTimeout(() => {
        //         [...document.body.childNodes].forEach(node => {
        //
        //             if (
        //               node.nodeType === 1 &&
        //               node.id?.startsWith('dmermaid-')
        //
        //             ) {
        //                 console.log(node);
        //               document.body.removeChild(node); // ✅ 正统做法
        //             }
        //           });
        //     }, 100);
        return `<div class="mermaid">\n${cleanCode}\n</div>`;
    }

    // 对于其他代码块，使用默认渲染逻辑
    const langClass = language ? ` class="language-${language}"` : '';
    return `<pre><code${langClass}>${code}</code></pre>`;
};
// 配置marked
marked.setOptions({
    sanitize: true,
    renderer: customRenderer,
    highlight: function(code, lang) {
        if (lang && hljs.getLanguage(lang)) {
            return hljs.highlight(code, { language: lang }).value;
        }
        return hljs.highlightAuto(code).value;
    },
    breaks: true,
});

// 配置mermaid
mermaid.initialize({
    startOnLoad: false,  // 改为false，由我们手动控制渲染
    theme: 'default',
    securityLevel: 'loose',
    flowchart: {
        htmlLabels: true,
        curve: 'basis',
        nodeSpacing: 100,
        rankSpacing: 80,
        padding: 20,
        // useMaxWidth: false  // 添加这个配置
    }
});

const MarkdownRenderer = React.memo(({ content,className="markdown-content" }) => {
    const containerRef = useRef(null);
    const mermaidCounter = useRef(0);

    useEffect(() => {
        const renderMermaidDiagrams = async () => {
            if (!containerRef.current) return;

            try {

                // 获取所有未处理的 mermaid 图表
                const mermaidDivs = containerRef.current.querySelectorAll('.mermaid');

                // 重新渲染所有图表
                for (const element of mermaidDivs) {
                    try {

                        let graphDefinition = element.textContent.trim();
                        if(!graphDefinition){
                            continue;
                        }

                        const id = `mermaid-${Date.now()}-${mermaidCounter.current++}`;
                        if(element.childNodes[0].nodeType !== Node.TEXT_NODE){
                            continue;
                        }

                        // 检查缓存中是否有相同的图表
                        if (diagramCache.has(graphDefinition)) {
                            const cachedNode = diagramCache.get(graphDefinition);
                            element.innerHTML = cachedNode.innerHTML;
                            continue;
                        }

                        const {svg} = await mermaid.render(id, graphDefinition);
                        element.innerHTML = svg;
                        // 将渲染好的 SVG 存入缓存
                        diagramCache.set(graphDefinition, element);

                    } catch (error) {
                        console.log(element);
                        console.error('Error rendering individual mermaid diagram',error);
                    }
                }
            } catch (error) {
                console.error('Mermaid rendering error:', error);
            }
        };

        // 使用 setTimeout 确保在 DOM 更新后再渲染
        setTimeout(renderMermaidDiagrams, 0);
    }, [content]);

    // 使用 useEffect 的清理函数来重置计数器和缓存
    useEffect(() => {
        mermaidCounter.current = 0;
        return () => {
            mermaidCounter.current = 0;
        };
    }, []);

    const renderMarkdown = (text) => {
        if (!text) return '';

        // 处理 <thinking></thinking> 标签
        text = text.replace(/<thinking>([\s\S]*?)<\/thinking>/g, (match, content) => {
            return `<div class="thinking">
                        <div class="thinking-header">
                            <span>思考中</span>
                            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="12" height="12" viewBox="0 0 12 12"><defs><clipPath id="master_svg0_280_72986"><rect x="0" y="0" width="12" height="12" rx="0"/></clipPath></defs><g clip-path="url(#master_svg0_280_72986)"><g><path d="M2.181732375,4.376006234375C2.400125375,4.157481634375,2.748755375,4.140517634375,2.9873323750000003,4.3368062343750005L3.030129375,4.375606234375L5.997329375,7.3428102343749995L8.944529375,4.356006234375C9.161649375,4.136036834375,9.510349375,4.116991734375,9.750129375,4.312006234375L9.793329374999999,4.350406234375C10.013299375,4.567525234375,10.032349375,4.916222234375,9.837329375,5.156006234375L9.798929375,5.198810234375L6.426929375,8.615610234375C6.208129375,8.837250234375,5.856099374999999,8.854630234375,5.616529375,8.655610234375L5.575729375,8.618410234375L2.181731375,5.224410234375C1.947235375,4.990060234375,1.947235375,4.609952234375,2.181731375,4.375606234375L2.181732375,4.376006234375Z" fill="#C9CDD4" fill-opacity="1"/></g></g></svg>
                        </div>
                        <div class="thinking-content">${content}</div>
                    </div>`;
        });

        // 处理 <execute_command></execute_command> 标签
        text = text.replace(/<execute_command>([\s\S]*?)<\/execute_command>/g, (match, content) => {
            content = content.replace(/<requires_approval>([\s\S]*?)<\/requires_approval>/g,"");
            const highlightedContent = hljs.highlightAuto(content.replace('<command>','').replace('</command>','')).value;
            return `<div class="tag"><span class="tag-header">执行命令</span><div class="tag-content">${highlightedContent}</div></div>`;
        });

        // 处理 <write_to_file></write_to_file> 标签
        text = text.replace(/<write_to_file>([\s\S]*?)<\/write_to_file>/g, (match, content) => {
            const pathMatch = content.match(/<path>([\s\S]*?)<\/path>/);
            const fileName = pathMatch ? pathMatch[1] : "";
            content = content.replace(/<path>([\s\S]*?)<\/path>/g, "");
            const highlightedContent = hljs.highlightAuto(content.replace('<content>','').replace('</content>','')).value;
            return `<div class="tag">
                        <span class="tag-header">
                            <span>写入文件</span>
                            <span class="fileName">
                                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="16" height="16" viewBox="0 0 16 16"><defs><clipPath id="master_svg0_228_245205"><rect x="16" y="0" width="16" height="16" rx="0"/></clipPath></defs><g transform="matrix(-1,0,0,1,32,0)" clip-path="url(#master_svg0_228_245205)"><g transform="matrix(0.7384299635887146,-0.6743301749229431,0.6743301749229431,0.7384299635887146,-1.0748418173901868,12.137206164551195)"><path d="M26.32119876499176,9.268419627990722C25.82979876499176,8.721379627990723,25.16089876499176,8.415602627990722,24.46382876499176,8.417005627990722L24.46116876499176,8.417005627990722C23.76681876499176,8.415602627990722,23.09917876499176,8.719979627990723,22.60912876499176,9.264209627990724L22.60778876499176,9.265619627990723L17.77006876499176,14.612559627990723C17.32007876499176,15.111899627990724,17.18654876499176,15.860929627990723,17.43089876499176,16.51175962799072C17.67525876499176,17.162589627990723,18.25076876499176,17.586179627990724,18.88902876499176,17.58477962799072C19.29228876499176,17.58477962799072,19.69420876499176,17.416469627990722,19.99998876499176,17.077029627990722L24.83771876499176,11.730089627990722C25.043348764991762,11.502859627990723,25.043348764991762,11.135359627990724,24.83771876499176,10.908129627990723C24.63207876499176,10.680899627990723,24.29959876499176,10.680899627990723,24.09395876499176,10.908129627990723L19.25756876499176,16.25506962799072C19.15875876499176,16.36307962799072,19.02522876499176,16.423389627990723,18.886358764991762,16.423389627990723C18.74748876499176,16.423389627990723,18.61395876499176,16.36166962799072,18.51648876499176,16.25226962799072C18.41633876499176,16.145659627990725,18.36025876499176,15.998379627990722,18.35892876499176,15.844089627990723C18.35892876499176,15.689799627990723,18.413668764991762,15.542519627990723,18.512478764991762,15.434509627990723L23.350208764991763,10.087569627990723C23.64396876499176,9.760749627990723,24.044558764991763,9.578409627990723,24.46116876499176,9.579809627990723L24.46249876499176,9.579809627990723C24.88043876499176,9.578409627990723,25.28239876499176,9.763559627990723,25.57749876499176,10.090379627990723C25.875198764991758,10.418599627990723,26.039498764991762,10.857629627990722,26.039498764991762,11.321919627990724C26.04079876499176,11.783389627990722,25.875198764991758,12.226629627990722,25.58009876499176,12.552049627990723L24.27421876499176,13.995389627990722L24.26620876499176,14.003799627990723L20.64625876499176,18.006979627990724C19.61942876499176,19.140379627990722,17.95566876499176,19.141779627990722,16.92749876499176,18.00837962799072C16.43344876499176,17.464179627990724,16.15704876499176,16.72495962799072,16.15971876499176,15.954899627990722C16.15971876499176,15.177829627990722,16.43344876499176,14.447039627990723,16.93016876499176,13.898599627990723L21.86270876499176,8.446461627990722C22.06833876499176,8.219230627990722,22.06833876499176,7.851733627990723,21.86270876499176,7.624502627990722C21.65706876499176,7.397271927990722,21.32458876499176,7.397271927990722,21.11894876499176,7.624502627990722L16.18641876499176,13.076639627990723C15.49340976499176,13.838289627990722,15.104842194991761,14.874849627990724,15.10751272169176,15.954899627990722C15.10751272169176,17.04195962799072,15.48940376499176,18.06447962799072,16.183748764991762,18.830379627990723C16.87275876499176,19.59337962799072,17.80878876499176,20.021179627990723,18.78487876499176,20.018379627990722C19.76230876499176,20.019779627990722,20.69967876499176,19.591979627990725,21.39001876499176,18.827579627990723L26.50549876499176,13.173429627990723C26.556198764991763,13.115919627990722,26.596298764991758,13.047189627990722,26.621698764991763,12.972849627990723C26.92609876499176,12.490329627990722,27.09169876499176,11.920849627990723,27.09169876499176,11.320509627990722C27.092998764991762,10.550449627990723,26.81529876499176,9.811249627990723,26.32119876499176,9.268419627990722Z" fill="currentColor" fill-opacity="1" style="mix-blend-mode:passthrough"/></g></g></svg>
                                <span>${fileName}</span>
                            </span>
                        </span>
                        <div class="tag-content">${highlightedContent}</div>
                    </div>`;
        });

        // 处理 <list_code_definition_names></list_code_definition_names> 标签
        text = text.replace(/<list_code_definition_names>([\s\S]*?)<\/list_code_definition_names>/g, (match, content) => {
            const highlightedContent = hljs.highlightAuto(content).value;
            return `<div class="tag"><span class="tag-header">代码定义</span><div class="tag-content">${highlightedContent}</div></div>`;
        });

        // 处理 <read_file></read_file> 标签
        text = text.replace(/<read_file>([\s\S]*?)<\/read_file>/g, (match, content) => {
            return `<span class="tag-attr">读取文件</span>${content.trim()}`;
        });

        // 处理 <search_files></search_files> 标签
        text = text.replace(/<search_files>([\s\S]*?)<\/search_files>/g, (match, content) => {
            return `<span class="tag-attr">搜索文件</span>${content.trim()}`;
        });

        // 处理 <list_files></list_files> 标签
        text = text.replace(/<list_files>([\s\S]*?)<\/list_files>/g, (match, content) => {
            return `<span class="tag-attr">文件</span>${content.trim()}`;
        });

        // 处理 <ask_followup_question></ask_followup_question> 标签
        text = text.replace(/<ask_followup_question>([\s\S]*?)<\/ask_followup_question>/g, (match, content) => {
            return `<div class="tag-text">
                        <div class="tag-header">
                            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="20" height="20" viewBox="0 0 20 20"><defs><clipPath id="master_svg0_280_75002"><rect x="0" y="0" width="20" height="20" rx="0"/></clipPath></defs><g clip-path="url(#master_svg0_280_75002)"><g><path d="M10.09921875,1.35003662109375C5.32201875,1.35003662109375,1.44921875,5.22283662109375,1.44921875,10.00003662109375C1.44921875,14.77713662109375,5.32201875,18.65003662109375,10.09921875,18.65003662109375C14.87631875,18.65003662109375,18.74921875,14.77713662109375,18.74921875,10.00003662109375C18.74921875,5.22283662109375,14.87631875,1.35003662109375,10.09921875,1.35003662109375ZM10.44781875,15.10863662109375C10.26991875,15.27173662109375,10.06241875,15.36073662109375,9.81021875,15.36073662109375C9.55811875,15.36073662109375,9.350518749999999,15.27163662109375,9.17261875,15.10863662109375C8.99471875,14.93073662109375,8.92051875,14.72313662109375,8.92051875,14.47093662109375C8.92051875,14.21893662109375,8.99461875,14.01113662109375,9.17261875,13.84823662109375C9.350618749999999,13.67033662109375,9.55811875,13.59613662109375,9.81021875,13.59613662109375C10.06231875,13.59613662109375,10.28471875,13.67033662109375,10.46261875,13.84823662109375C10.62571875,14.01113662109375,10.71471875,14.21893662109375,10.71471875,14.47093662109375C10.71471875,14.73793662109375,10.62581875,14.94553662109375,10.44781875,15.10863662109375ZM12.58321875,9.17713662109375C12.43501875,9.35503662109375,12.03451875,9.74063662109375,11.38201875,10.31893662109375C11.05581875,10.60053662109375,10.83341875,10.86753662109375,10.68511875,11.13453662109375C10.49231875,11.46073662109375,10.40331875,11.81653662109375,10.40331875,12.21693662109375L10.40331875,12.55813662109375L9.217118750000001,12.55813662109375L9.217118750000001,12.21693662109375C9.217118750000001,11.72763662109375,9.30611875,11.29763662109375,9.48401875,10.92693662109375C9.69161875,10.48203662109375,10.18101875,9.91853662109375,10.98181875,9.20673662109375C11.21901875,8.96953662109375,11.39701875,8.79153662109375,11.48591875,8.67293662109375C11.78251875,8.302236621093751,11.93071875,7.91663662109375,11.93071875,7.50143662109375C11.93071875,6.90833662109375,11.75291875,6.44863662109375,11.42661875,6.12243662109375C11.08561875,5.76653662109375,10.59611875,5.60353662109375,9.97341875,5.60353662109375C9.23201875,5.60353662109375,8.683418750000001,5.84073662109375,8.32751875,6.33013662109375C8.00121875,6.74533662109375,7.83811875,7.30883662109375,7.83811875,8.03543662109375L6.66651875,8.03543662109375C6.66651875,6.99743662109375,6.94831875,6.16703662109375,7.54141875,5.54433662109375C8.13461875,4.89183662109375,8.964918749999999,4.5655366210937505,10.03261875,4.5655366210937505C10.98171875,4.5655366210937505,11.73791875,4.81763662109375,12.31631875,5.35133662109375C12.86491875,5.85553662109375,13.14671875,6.55253662109375,13.14671875,7.44213662109375C13.14671875,8.10943662109375,12.95391875,8.68773662109375,12.58321875,9.17713662109375Z" fill="#FFFFFF" fill-opacity="1" style="mix-blend-mode:passthrough"/></g></g></svg>
                            <span>提出后续问题</span>
                        </div>
                        <div class="tag-content">${content}</div>
                    </div>`;
        });

        // 处理 <attempt_completion></attempt_completion> 标签
        text = text.replace(/<attempt_completion>([\s\S]*?)<\/attempt_completion>/g, (match, content) => {
            return `<div class="tag-text">
                        <div class="tag-header">
                            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="20" height="20" viewBox="0 0 20 20"><defs><clipPath id="master_svg0_280_75002"><rect x="0" y="0" width="20" height="20" rx="0"/></clipPath></defs><g clip-path="url(#master_svg0_280_75002)"><g><path d="M10.09921875,1.35003662109375C5.32201875,1.35003662109375,1.44921875,5.22283662109375,1.44921875,10.00003662109375C1.44921875,14.77713662109375,5.32201875,18.65003662109375,10.09921875,18.65003662109375C14.87631875,18.65003662109375,18.74921875,14.77713662109375,18.74921875,10.00003662109375C18.74921875,5.22283662109375,14.87631875,1.35003662109375,10.09921875,1.35003662109375ZM10.44781875,15.10863662109375C10.26991875,15.27173662109375,10.06241875,15.36073662109375,9.81021875,15.36073662109375C9.55811875,15.36073662109375,9.350518749999999,15.27163662109375,9.17261875,15.10863662109375C8.99471875,14.93073662109375,8.92051875,14.72313662109375,8.92051875,14.47093662109375C8.92051875,14.21893662109375,8.99461875,14.01113662109375,9.17261875,13.84823662109375C9.350618749999999,13.67033662109375,9.55811875,13.59613662109375,9.81021875,13.59613662109375C10.06231875,13.59613662109375,10.28471875,13.67033662109375,10.46261875,13.84823662109375C10.62571875,14.01113662109375,10.71471875,14.21893662109375,10.71471875,14.47093662109375C10.71471875,14.73793662109375,10.62581875,14.94553662109375,10.44781875,15.10863662109375ZM12.58321875,9.17713662109375C12.43501875,9.35503662109375,12.03451875,9.74063662109375,11.38201875,10.31893662109375C11.05581875,10.60053662109375,10.83341875,10.86753662109375,10.68511875,11.13453662109375C10.49231875,11.46073662109375,10.40331875,11.81653662109375,10.40331875,12.21693662109375L10.40331875,12.55813662109375L9.217118750000001,12.55813662109375L9.217118750000001,12.21693662109375C9.217118750000001,11.72763662109375,9.30611875,11.29763662109375,9.48401875,10.92693662109375C9.69161875,10.48203662109375,10.18101875,9.91853662109375,10.98181875,9.20673662109375C11.21901875,8.96953662109375,11.39701875,8.79153662109375,11.48591875,8.67293662109375C11.78251875,8.302236621093751,11.93071875,7.91663662109375,11.93071875,7.50143662109375C11.93071875,6.90833662109375,11.75291875,6.44863662109375,11.42661875,6.12243662109375C11.08561875,5.76653662109375,10.59611875,5.60353662109375,9.97341875,5.60353662109375C9.23201875,5.60353662109375,8.683418750000001,5.84073662109375,8.32751875,6.33013662109375C8.00121875,6.74533662109375,7.83811875,7.30883662109375,7.83811875,8.03543662109375L6.66651875,8.03543662109375C6.66651875,6.99743662109375,6.94831875,6.16703662109375,7.54141875,5.54433662109375C8.13461875,4.89183662109375,8.964918749999999,4.5655366210937505,10.03261875,4.5655366210937505C10.98171875,4.5655366210937505,11.73791875,4.81763662109375,12.31631875,5.35133662109375C12.86491875,5.85553662109375,13.14671875,6.55253662109375,13.14671875,7.44213662109375C13.14671875,8.10943662109375,12.95391875,8.68773662109375,12.58321875,9.17713662109375Z" fill="#FFFFFF" fill-opacity="1" style="mix-blend-mode:passthrough"/></g></g></svg>
                            <span>尝试完成任务</span>
                        </div>
                        <div class="tag-content">${content}</div>
                    </div>`;
        });

        // 处理 <replace_in_file></replace_in_file> 标签
        text = text.replace(/<replace_in_file>([\s\S]*?)<\/replace_in_file>/g, (match, content) => {
            content = content.replace(/<path>([\s\S]*?)<\/path>/g, (m, con) => {
                return `<span class="tag-header">
                            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="16" height="16" viewBox="0 0 16 16"><defs><clipPath id="master_svg0_228_245205"><rect x="16" y="0" width="16" height="16" rx="0"/></clipPath></defs><g transform="matrix(-1,0,0,1,32,0)" clip-path="url(#master_svg0_228_245205)"><g transform="matrix(0.7384299635887146,-0.6743301749229431,0.6743301749229431,0.7384299635887146,-1.0748418173901868,12.137206164551195)"><path d="M26.32119876499176,9.268419627990722C25.82979876499176,8.721379627990723,25.16089876499176,8.415602627990722,24.46382876499176,8.417005627990722L24.46116876499176,8.417005627990722C23.76681876499176,8.415602627990722,23.09917876499176,8.719979627990723,22.60912876499176,9.264209627990724L22.60778876499176,9.265619627990723L17.77006876499176,14.612559627990723C17.32007876499176,15.111899627990724,17.18654876499176,15.860929627990723,17.43089876499176,16.51175962799072C17.67525876499176,17.162589627990723,18.25076876499176,17.586179627990724,18.88902876499176,17.58477962799072C19.29228876499176,17.58477962799072,19.69420876499176,17.416469627990722,19.99998876499176,17.077029627990722L24.83771876499176,11.730089627990722C25.043348764991762,11.502859627990723,25.043348764991762,11.135359627990724,24.83771876499176,10.908129627990723C24.63207876499176,10.680899627990723,24.29959876499176,10.680899627990723,24.09395876499176,10.908129627990723L19.25756876499176,16.25506962799072C19.15875876499176,16.36307962799072,19.02522876499176,16.423389627990723,18.886358764991762,16.423389627990723C18.74748876499176,16.423389627990723,18.61395876499176,16.36166962799072,18.51648876499176,16.25226962799072C18.41633876499176,16.145659627990725,18.36025876499176,15.998379627990722,18.35892876499176,15.844089627990723C18.35892876499176,15.689799627990723,18.413668764991762,15.542519627990723,18.512478764991762,15.434509627990723L23.350208764991763,10.087569627990723C23.64396876499176,9.760749627990723,24.044558764991763,9.578409627990723,24.46116876499176,9.579809627990723L24.46249876499176,9.579809627990723C24.88043876499176,9.578409627990723,25.28239876499176,9.763559627990723,25.57749876499176,10.090379627990723C25.875198764991758,10.418599627990723,26.039498764991762,10.857629627990722,26.039498764991762,11.321919627990724C26.04079876499176,11.783389627990722,25.875198764991758,12.226629627990722,25.58009876499176,12.552049627990723L24.27421876499176,13.995389627990722L24.26620876499176,14.003799627990723L20.64625876499176,18.006979627990724C19.61942876499176,19.140379627990722,17.95566876499176,19.141779627990722,16.92749876499176,18.00837962799072C16.43344876499176,17.464179627990724,16.15704876499176,16.72495962799072,16.15971876499176,15.954899627990722C16.15971876499176,15.177829627990722,16.43344876499176,14.447039627990723,16.93016876499176,13.898599627990723L21.86270876499176,8.446461627990722C22.06833876499176,8.219230627990722,22.06833876499176,7.851733627990723,21.86270876499176,7.624502627990722C21.65706876499176,7.397271927990722,21.32458876499176,7.397271927990722,21.11894876499176,7.624502627990722L16.18641876499176,13.076639627990723C15.49340976499176,13.838289627990722,15.104842194991761,14.874849627990724,15.10751272169176,15.954899627990722C15.10751272169176,17.04195962799072,15.48940376499176,18.06447962799072,16.183748764991762,18.830379627990723C16.87275876499176,19.59337962799072,17.80878876499176,20.021179627990723,18.78487876499176,20.018379627990722C19.76230876499176,20.019779627990722,20.69967876499176,19.591979627990725,21.39001876499176,18.827579627990723L26.50549876499176,13.173429627990723C26.556198764991763,13.115919627990722,26.596298764991758,13.047189627990722,26.621698764991763,12.972849627990723C26.92609876499176,12.490329627990722,27.09169876499176,11.920849627990723,27.09169876499176,11.320509627990722C27.092998764991762,10.550449627990723,26.81529876499176,9.811249627990723,26.32119876499176,9.268419627990722Z" fill="currentColor" fill-opacity="1" style="mix-blend-mode:passthrough"/></g></g></svg>
                            ${con}
                        </span>`;
            });
            content = content.replace(/<<<<<<< SEARCH\n([\s\S]*?)>>>>>>> REPLACE\n/g, (m, con) => {
                con = con.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g,"<br/>");
                let split = con.split('=======<br/>');
                return `<div class="replaceDiv">
                            <div class="box">
                                <span class="title">原内容</span>
                                <span class="con">${split[0]}</span>
                            </div>
                            <div class="box">
                                <span class="title">替换内容</span>
                                <span class="con">${split[1]}</span>
                            </div>
                        </div>`;
            });
            content = content.replace(/\n/g,"");
            return `<div class="tag-replace">${content}</div>`;
        });

        return marked(text);
    };

    const rawHtml = renderMarkdown(content);

    // 预处理 HTML，移除 link 标签
    const processedHtml = rawHtml.replace(/<link[^>]*>/g, '');

    return (
        <div
            ref={containerRef}
            className={className}
            dangerouslySetInnerHTML={{ __html: processedHtml }}
        />
    );
});

export default MarkdownRenderer;
